package com.jnevision.role.controller;

import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.jnevision.role.model.Role;
/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author gavin
 * @since 2019-07-13
 */
import com.jnevision.role.service.RoleServiceImpl;
import com.jnevision.util.DateUtil;

/**
 * 系统功能-角色
 * 
 * @author Gavin
 * @date 2019/07/13
 */

@RestController
@RequestMapping("/roles")
public class RoleController extends ApiController {
    @Autowired
    private RoleServiceImpl roleServiceImpl;

    @PostMapping
    public R<Boolean> add(@RequestBody Role role) {
        role.setValid(true);
        return this.success(this.roleServiceImpl.save(role));
    }

    @PutMapping
    public R<Boolean> update(@RequestBody Role role) {
        role.setUpdateTime(DateUtil.getNormalTime());
        return this.success(this.roleServiceImpl.updateById(role));
    }

    @DeleteMapping
    public R<Boolean> delete(int id) {
        return this.success(this.roleServiceImpl.removeById(id));
    }

    @GetMapping
    public R<Role> get(int id) {
        return this.success(this.roleServiceImpl.getById(id));
    }

    @RequestMapping("/all")
    public R<List<Role>> getAll() {
        return this.success(this.roleServiceImpl.list());
    }

    /**
     * 根据用户查询角色
     * 
     * @param userId
     * @return
     */
    @GetMapping("/{userId}")
    public R<Collection<Role>> getRole(@PathVariable String userId) {
        return this.success(this.roleServiceImpl.getRole(userId));
    }
}
